package servlet;

import dao.DaoFactory;
import entity.Exam;
import entity.Notice;
import utils.PathUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/notice")
public class NoticeServlet extends HttpServlet {

    private static final long serialVersionUID = 1057246510775782702L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String method = req.getParameter("method");
       if("listNotice".equals(method)){
           this.listNotice(req,resp);
       }else if("add".equals(method)){
           this.add(req,resp);
       }else if("delete".equals(method)){
           this.delete(req,resp);
       }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp){
          Integer n_id = getIntParameter(req,"n_id");
        try {
            DaoFactory.getInstance().getNoticeDao().delete(n_id);
            resp.sendRedirect(PathUtils.getBasePath(req)+"notice?method=listNotice&msg=删除消息成功!");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp){
        Exam exam = (Exam)req.getSession().getAttribute("exam");
        Integer e_id = exam.getE_id();
        String n_text = req.getParameter("n_text");
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(System.currentTimeMillis());
        String n_time =format.format(date);

        Notice notice = new Notice();

        notice.setE_id(e_id);
        notice.setN_text(n_text);
        notice.setN_time(n_time);

        try {
            DaoFactory.getInstance().getNoticeDao().add(notice);
            resp.sendRedirect(PathUtils.getBasePath(req)+"notice?method=listNotice&msg=添加消息成功!");
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }

    }

    private void listNotice(HttpServletRequest req, HttpServletResponse resp){
        Exam exam = (Exam)req.getSession().getAttribute("exam");
        Integer e_id = exam.getE_id();
        try {
            List<Notice> notices = DaoFactory.getInstance().getNoticeDao().listNotice(e_id);
            req.setAttribute("noticeList", notices);
            req.getRequestDispatcher("page/Exam/notice.jsp").forward(req, resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    public Integer getIntParameter(HttpServletRequest request,String name) {
        if(StringUtils.isNoneBlank(request.getParameter(name))) {
            return Integer.parseInt(request.getParameter(name));
        }else {
            return null;
        }
    }
}
